<?php

class Light_Paginator implements Countable, IteratorAggregate
{
	/**
	 * @var Light_Database | array
	 */
    protected $_model;

    /**
     * @var ArrayIterator
     */
    protected $_currentItems;

    /**
     * @var int
     */
    protected $_pageCount;

    /**
     * @var int
     */
    protected $_ipp = 10;

    /**
     * @var int
     */
    protected $_pageNumber = 1;

    /**
     * @var int
     */
    protected $_pageRange = 10;

    /**
     * @var int
     */
    protected $_totalItemCount;

    /**
     * @param Light_Database | array $model
     */
    public function __construct($model)
    {
        $this->_model = $model;
    }

	/* (non-PHPdoc)
     * @see IteratorAggregate::getIterator()
     */
    public function getIterator ()
    {
        if ($this->_currentItems === null) {

            $offset = ($this->normalizePageNumber($this->getPageNumber()) - 1) * $this->getIpp();

            if (is_array($this->_model)) {
                $items = array_slice($this->_model, $offset, $this->getIpp());
            } else {
                $items = $this->_model->paginate($offset, $this->getIpp());
            }

            if (!$items instanceof Traversable) {
                $items = new ArrayIterator($items);
            }

            $this->_currentItems = $items;

        }

        return $this->_currentItems;
    }

	/* (non-PHPdoc)
     * @see Countable::count()
     */
    public function count ()
    {
        if ($this->_pageCount === null) {
        	$this->_pageCount = ceil($this->getTotalItemCount() / $this->getIpp());
        }

        return $this->_pageCount;
    }

    /**
     * @return number
     */
    public function getTotalItemCount()
    {
    	if ($this->_totalItemCount === null) {
    		if (is_array($this->_model)) {
    			$this->_totalItemCount = count($this->_model);
    		} else {
    			$this->_totalItemCount = $this->_model->paginateCount();
    		}
    	}

    	return $this->_totalItemCount;
    }

	/**
     * @return int
     */
    public function getIpp ()
    {
        return $this->_ipp;
    }

	/**
	 * @param int $_ipp
	 * @return Light_Paginator
	 */
    public function setIpp ($_ipp)
    {
        $this->_ipp = $_ipp;

        return $this;
    }

	/**
     * @return int
     */
    public function getPageNumber ()
    {
        return $this->_pageNumber;
    }

	/**
	 * @param int $_pageNumber
	 * @return Light_Paginator
	 */
    public function setPageNumber ($_pageNumber)
    {
        $this->_pageNumber = $_pageNumber;

        return $this;
    }

    /**
	 * @return int
	 */
	public function getPageRange()
	{
		return $this->_pageRange;
	}

	/**
	 * @param int $_pageRange
	 * @return Light_Paginator
	 */
	public function setPageRange($_pageRange)
	{
		$this->_pageRange = $_pageRange;

		return $this;
	}

	/**
     * @return stdClass
     */
    public function getPages()
    {
        $pageCount         = $this->count();
        $currentPageNumber = $this->getPageNumber();

        $pages = new stdClass();
        $pages->pageCount        = $pageCount;
        $pages->itemCountPerPage = $this->getIpp();
        $pages->first            = 1;
        $pages->current          = $currentPageNumber;
        $pages->last             = $pageCount;

        // Previous and next
        if ($currentPageNumber - 1 > 0) {
            $pages->previous = $currentPageNumber - 1;
        }

        if ($currentPageNumber + 1 <= $pageCount) {
            $pages->next = $currentPageNumber + 1;
        }

        // Pages in range
        $pages->pagesInRange     = $this->_slidingPages();
        $pages->firstPageInRange = min($pages->pagesInRange);
        $pages->lastPageInRange  = max($pages->pagesInRange);
        $pages->totalItemCount   = $this->getTotalItemCount();

        if ($this->_currentItems !== null) {
            $pages->currentItemCount = count($this->_currentItems);
            $pages->itemCountPerPage = $this->getIpp();
            $pages->firstItemNumber  = (($currentPageNumber - 1) * $this->getIpp()) + 1;
            $pages->lastItemNumber   = $pages->firstItemNumber + $pages->currentItemCount - 1;
        }

        return $pages;
    }

    /**
     * @param int $pageRange
     */
    protected function _slidingPages()
    {
        $pageRange  = $this->getPageRange();
        $pageNumber = $this->getPageNumber();
        $pageCount  = $this->count();

        if ($pageRange > $pageCount) {
            $pageRange = $pageCount;
        }

        $delta = ceil($pageRange / 2);

        if ($pageNumber - $delta > $pageCount - $pageRange) {
            $lowerBound = $pageCount - $pageRange + 1;
            $upperBound = $pageCount;
        } else {
            if ($pageNumber - $delta < 0) {
                $delta = $pageNumber;
            }
            $offset     = $pageNumber - $delta;
            $lowerBound = $offset + 1;
            $upperBound = $offset + $pageRange;
        }

        $lowerBound = $this->normalizePageNumber($lowerBound);
        $upperBound = $this->normalizePageNumber($upperBound);

        $pages = array();

        for ($pageNumber = $lowerBound; $pageNumber <= $upperBound; $pageNumber++) {
            $pages[$pageNumber] = $pageNumber;
        }

        return $pages;
    }

    /**
     * @param int $pageNumber
     * @return number
     */
    public function normalizePageNumber($pageNumber)
    {
        if ($pageNumber < 1) {
            $pageNumber = 1;
        }

        $pageCount = $this->count();

        if ($pageCount > 0 && $pageNumber > $pageCount) {
            $pageNumber = $pageCount;
        }

        return $pageNumber;
    }

    /**
     * @param Light_View $view
     * @param string $viewScriptName
     * @return string
     */
    public function renderPages(Light_View $view, $viewScriptName = 'paginator')
    {
        $pages = $this->getPages();

        $paginatorView = clone $view;

        $paginatorView->pages = $pages;

        return $paginatorView->render($viewScriptName);
    }
}